function rule(regx,value,infoDiv,errorInfo) {
    let b = regx.test(value);
    if (!b){
        $(infoDiv).innerHTML = errorInfo;
        $(infoDiv).style.color = "red"
    }else {
        $(infoDiv).innerHTML = "√";
        $(infoDiv).style.color = "green";
        return true;
    }
}

function ruleName(){
    let regx = /^\w{1,10}$/;
    let value = $("pwd").value;
    let b = rule(regx,value,"pwdInfo","❌");
    if (b){
        return true;
    }
}

function ruleEmail(){
    let regx = /^\w{2,10}@\w{2}.com$/;
    let value = $("email").value;
    let s = rule(regx,value,"emailSpan","❌");
    if (s){
        return true;
    }
}


async function checkName() {
    let boolean = await ajaxGet("/user/checkName",{username:$("username").value});
    // console.log(boolean);
    if (boolean == "ok"){
        $("nameInfo").innerHTML= "已存在";
        $("nameInfo").style.color = "red"
    }else {
        $("nameInfo").innerHTML= "√";
        $("nameInfo").style.color = "green";
        return true;
    }
    // ruleName()
}

function checkPwd() {
    let pwd = $("pwd").value;
    let againPwd = $("againPwd").value;
    if (pwd != againPwd){
        $("pwdSpan").innerHTML= "两次密码不一致";
        $("pwdSpan").style.color = "red"
    }else {
        $("pwdSpan").innerHTML= "√";
        $("pwdSpan").style.color = "green";
        return true;
    }
}

function usubmit() {
    var radionum = document.getElementById("radioDiv").img;
    for (var i = 0; i < radionum.length; i++) {
        if (radionum[i].checked) {
            return radionum[i].value
        }
    }
}


async function addUser() {
    if (ruleName() && ruleEmail() && checkName() && checkPwd()){
        let user = await ajaxGet("/user/add",{
            username:$("username").value,
            pwd:$("pwd").value,
            email:$("email").value,
            facePath:usubmit()
        });
        if (user == "ok"){
            location.href = "/html/login.html";
        }else {
            alert("添加失败")
        }
    }
}